﻿using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using Token.Piker.BLL;
using Token.Piker.Core;

namespace Token.Piker.Web
{
    public partial class Classify : PikerBasePage
    {
        private static bool isAlbum = false;//显示单张还是组图的标识
        private static string strWhere = string.Empty;
        private TagBLL tagBLL = new TagBLL();
        private PhotoBLL photoBLL = new PhotoBLL();
        private AlbumBLL albumBLL = new AlbumBLL();
        private UserBLL userBLL = new UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTagNames();

                if (isAlbum)
                {
                    BindAlbumInfo();
                }
                else
                {
                    BindPhotoInfo();
                }
            }
        }

        protected void lkbPhoto_Click(object sender, EventArgs e)
        {
            if (isAlbum)
            {
                isAlbum = false;
                strWhere = string.Empty;
                BindPhotoInfo();
            }
        }

        protected void lkbAlbum_Click(object sender, EventArgs e)
        {
            if (!isAlbum)
            {
                isAlbum = true;
                strWhere = string.Empty;
                BindAlbumInfo();
            }
        }

        protected void lkbAllTags_Click(object sender, EventArgs e)
        {
            strWhere = string.Empty;

            if (isAlbum)
            {
                pagerList.CurrentPageIndex = 1;
                BindAlbumInfo();
            }
            else
            {
                pagerList.CurrentPageIndex = 1;
                BindPhotoInfo();
            }
        }

        protected void lkbContentTags_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            if (isAlbum)
            {
                //取得所有内容标签对应的相册ID的列表
                DataSet ds = tagBLL.GetAllAlbumIDListOfOneTypeOfTag(0);
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    sb.Append("ID IN (");

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        sb.Append(dr["Album"]);
                        sb.Append(",");
                    }

                    sb.Remove(sb.Length - 1, 1);
                    sb.Append(")");
                    strWhere = sb.ToString();
                    pagerList.CurrentPageIndex = 1;
                    BindAlbumInfo();
                }
                else
                {
                    rptAlbumList.DataSource = null;
                    rptAlbumList.DataBind();
                    rptPhotoList.DataSource = null;
                    rptPhotoList.DataBind();
                    pagerList.RecordCount = 0;
                }
            }
            else
            {
                //取得所有内容标签对应的单张图片ID的列表
                DataSet ds = tagBLL.GetAllPhotoIDListOfOneTypeOfTag(0);
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    sb.Append("ID IN (");

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        sb.Append(dr["ID"]);
                        sb.Append(",");
                    }

                    sb.Remove(sb.Length - 1, 1);
                    sb.Append(")");
                    strWhere = sb.ToString();

                    pagerList.CurrentPageIndex = 1;
                    BindPhotoInfo();
                }
                else
                {
                    rptAlbumList.DataSource = null;
                    rptAlbumList.DataBind();
                    rptPhotoList.DataSource = null;
                    rptPhotoList.DataBind();
                    pagerList.RecordCount = 0;
                }
            }
        }

        protected void lkbStyleTags_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            if (isAlbum)
            {
                //取得所有内容标签对应的相册ID的列表
                DataSet ds = tagBLL.GetAllAlbumIDListOfOneTypeOfTag(1);
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    sb.Append("ID IN (");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        sb.Append(dr["Album"]);
                        sb.Append(",");
                    }

                    sb.Remove(sb.Length - 1, 1);
                    sb.Append(")");

                    strWhere = sb.ToString();
                    pagerList.CurrentPageIndex = 1;
                    BindAlbumInfo();
                }
                else
                {
                    rptPhotoList.DataSource = null;
                    rptPhotoList.DataBind();
                    rptAlbumList.DataSource = null;
                    rptAlbumList.DataBind();
                    pagerList.RecordCount = 0;
                }
            }
            else
            {
                //取得所有风格标签对应的单张图片ID的列表
                DataSet ds = tagBLL.GetAllPhotoIDListOfOneTypeOfTag(1);
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    sb.Append("ID IN (");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        sb.Append(dr["ID"]);
                        sb.Append(",");
                    }

                    sb.Remove(sb.Length - 1, 1);
                    sb.Append(")");
                    strWhere = sb.ToString();
                    pagerList.CurrentPageIndex = 1;
                    BindPhotoInfo();
                }
                else
                {
                    rptPhotoList.DataSource = null;
                    rptPhotoList.DataBind();
                    rptAlbumList.DataSource = null;
                    rptAlbumList.DataBind();
                    pagerList.RecordCount = 0;
                }
            }
        }

        protected void pagerList_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsCallback)
            {
                if (isAlbum)
                {
                    BindAlbumInfo();
                }
                else
                {
                    BindPhotoInfo();
                }
            }
        }

        private void BindTagNames()
        {
            DataSet ds = tagBLL.GetAllList();
            DataTable dtContent = ds.Tables[0].Clone();
            DataTable dtStyle = ds.Tables[0].Clone();
            ds.Tables[0].Select("Flag = 0").CopyToDataTable(dtContent, LoadOption.Upsert);
            ds.Tables[0].Select("Flag = 1").CopyToDataTable(dtStyle, LoadOption.Upsert);
            rptContentTags.DataSource = dtContent;
            rptContentTags.DataBind();
            rptStyleTags.DataSource = dtStyle;
            rptStyleTags.DataBind();
        }

        /// <summary>
        /// 绑定单张信息
        /// </summary>
        private void BindPhotoInfo()
        {
            StringBuilder sb = new StringBuilder();

            if (strWhere != string.Empty)
            {
                sb.Append(strWhere);
                sb.Append(" AND ");
            }

            sb.Append("Flag=0 AND (Album IS NULL OR Album = '')");
            DataSet ds = photoBLL.GetListByPage(sb.ToString(), "UploadTime DESC", pagerList.StartRecordIndex, pagerList.StartRecordIndex + pagerList.PageSize - 1);
            rptPhotoList.DataSource = ds.Tables[0];
            rptPhotoList.DataBind();

            rptAlbumList.DataSource = null;
            rptAlbumList.DataBind();

            pagerList.RecordCount = photoBLL.GetRecordCount(sb.ToString());
        }

        /// <summary>
        /// 绑定组图信息
        /// </summary>
        private void BindAlbumInfo()
        {
            StringBuilder sb = new StringBuilder();

            if (strWhere != string.Empty)
            {
                sb.Append(strWhere);
                sb.Append(" AND ");
            }

            sb.Append("Flag=0");
            DataSet ds = albumBLL.GetListByPage(sb.ToString(), "CreateTime DESC", pagerList.StartRecordIndex, pagerList.StartRecordIndex + pagerList.PageSize - 1);
            rptAlbumList.DataSource = ds.Tables[0];
            rptAlbumList.DataBind();

            rptPhotoList.DataSource = null;
            rptPhotoList.DataBind();

            pagerList.RecordCount = albumBLL.GetRecordCount(sb.ToString());
        }

        protected string GetUsernameByID(object userID)
        {
            int uid = Convert.ToInt32(userID);
            Model.User user = userBLL.GetModel(uid);
            return user.UserName;
        }

        protected void Tags_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ChangeTag")
            {
                int tid = Convert.ToInt32(e.CommandArgument);

                StringBuilder sb = new StringBuilder();

                if (isAlbum)
                {
                    DataSet ds = tagBLL.GetAllAlbumIDListOfOneTag(tid);
                    DataTable dt = ds.Tables[0];

                    if (dt.Rows.Count > 0)
                    {
                        sb.Append("ID IN (");

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            DataRow dr = dt.Rows[i];
                            sb.Append(dr["Album"]);
                            sb.Append(",");
                        }

                        sb.Remove(sb.Length - 1, 1);
                        sb.Append(")");

                        strWhere = sb.ToString();

                        pagerList.CurrentPageIndex = 1;
                        BindAlbumInfo();
                    }
                    else
                    {
                        rptAlbumList.DataSource = null;
                        rptAlbumList.DataBind();
                        rptPhotoList.DataSource = null;
                        rptPhotoList.DataBind();
                        pagerList.RecordCount = 0;
                    }
                }
                else
                {
                    DataSet ds = tagBLL.GetAllPhotoIDListOfOneTag(tid);
                    DataTable dt = ds.Tables[0];

                    if (dt.Rows.Count > 0)
                    {
                        sb.Append("ID IN (");
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            DataRow dr = dt.Rows[i];
                            sb.Append(dr["ID"]);
                            sb.Append(",");
                        }

                        sb.Remove(sb.Length - 1, 1);
                        sb.Append(")");

                        strWhere = sb.ToString();

                        pagerList.CurrentPageIndex = 1;
                        BindPhotoInfo();
                    }
                    else
                    {
                        rptAlbumList.DataSource = null;
                        rptAlbumList.DataBind();
                        rptPhotoList.DataSource = null;
                        rptPhotoList.DataBind();
                        pagerList.RecordCount = 0;
                    }
                }
            }
        }
    }
}